package com.atao.baokede.AiTools;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * 网页抓取工具
 */
public class WebScrapingTool {

    @Tool(description = """
            View the content of a web page from the specified URL or Fetches the complete HTML content of a web page from the specified URL.
            This tool is designed to retrieve static HTML data from web pages and return it as a string.
            It uses the Jsoup library for HTTP connection and parsing.
            Ensure that the target website allows web scraping (compliant with robots.txt and legal regulations).""")
    public String scrapeWebPage(@ToolParam(description = """
            The URL of the target web page to be scraped.
            It must be a valid HTTP or HTTPS link.
            Invalid or inaccessible URLs will result in an error message.
            Example: 'https://example.com'.
            Note: This tool may not work for dynamically loaded content via JavaScript.""")
                                String url) {
        try {
            Document document = Jsoup.connect(url).get();
            return document.html();
        } catch (Exception e) {
            return "Error scraping web page: " + e.getMessage();
        }
    }

}
